package com.relax.netty.tcp.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.charset.StandardCharsets;

public class MyTcpClinetHandler extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //todo 发送数据
//        final ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
//        byteBuf.writeShort(4);
//        byteBuf.writeBytes(("aaaa").getBytes(StandardCharsets.UTF_8));
//        ctx.writeAndFlush(byteBuf);
//
//
//        final ByteBuf byteBuf2 = ByteBufAllocator.DEFAULT.buffer();
//        byteBuf2.writeShort(4);
//        byteBuf2.writeBytes(("bbbb").getBytes(StandardCharsets.UTF_8));
//        ctx.writeAndFlush(byteBuf2);

//        final ByteBuf byteBuf3 = ByteBufAllocator.DEFAULT.buffer();
//        byteBuf3.writeShort(4);
//        ctx.writeAndFlush(byteBuf3);
//
//        final ByteBuf byteBuf4 = ByteBufAllocator.DEFAULT.buffer();
//        byteBuf4.writeBytes(("cccc").getBytes(StandardCharsets.UTF_8));
//        ctx.writeAndFlush(byteBuf4);


        // 换行分隔符

//        final ByteBuf byteBuf4 = ByteBufAllocator.DEFAULT.buffer();
//        final ByteBuf byteBuf5 = ByteBufAllocator.DEFAULT.buffer();
//        byteBuf4.writeShort(4);
//        byteBuf4.writeBytes(("dddd").getBytes(StandardCharsets.UTF_8));
//        byteBuf4.writeBytes(("\n").getBytes(StandardCharsets.UTF_8));
//        byteBuf4.writeShort(4);
//        byteBuf4.writeBytes(("ee").getBytes(StandardCharsets.UTF_8));
//        ctx.writeAndFlush(byteBuf4);

        String data ="REGISTER sip:34020000002000000170@3402000000 SIP/2.0\n" +
                "Via: SIP/2.0/UDP 172.20.44.105:2222;rport=2222;branch=z9hG4bK767402383;received=172.20.44.105\n" +
                "From: <sip:11020000002000000100@3402000000>;tag=1583068430\n" +
                "To: <sip:11020000002000000100@3402000000>\n" +
                "Call-ID: 8705d91e-4fd5-4df5-885c-a14900a7cf5c\n" +
                "CSeq: 103 REGISTER\n" +
                "Contact: <sip:11020000002000000100@172.20.51.234:5600>\n" +
                "Authorization: Digest username=\"11020000002000000100\",realm=\"3402000000\",nonce=\"ae2d8d87-f027-4e39-960d-aa563ef4ac51\",uri=\"sip:34020000002000000170@3402000000\",response=\"652b504332933a8fe8593f6e3ba4897f\",algorithm=MD5,cnonce=\"46aaf250-014c-4cb8-8f73-a4b0ade2c86e\",qop=auth,nc=00000001\n" +
                "Max-Forwards: 70\n" +
                "User-Agent: IP Camera\n" +
                "Expires: 3600\n" +
                "Content-Length: 0";

        ctx.writeAndFlush(data);




    }

    public static void main(String[] args) {
        System.out.println("彭青松".getBytes().length);
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println(System.currentTimeMillis() + "==>channelRead0");
        byte[] buffer = new byte[msg.readableBytes()];
        msg.readBytes(buffer);
        String message = new String(buffer, CharsetUtil.UTF_8);
        System.out.println("client 接收消息" + message);

        ctx.pipeline().write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println(System.currentTimeMillis() + "==>channelReadComplete");
        super.channelReadComplete(ctx);
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(System.currentTimeMillis() + "==>channelRegistered");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(System.currentTimeMillis() + "==>channelUnregistered");
        super.channelUnregistered(ctx);
    }
}
